<template>
  <div class="group-buy-fixed" :style="{ bottom: fixedOffset }">
    <div class="info">
      <div class="users">
        <img
          v-for="(v, i) in users"
          :key="i"
          :src="require(`@/assets/image/user/${v}`)"
        />
        <img src="@/assets/image/12.png" @click="handleClick" />
      </div>
      <div class="info-right">
        <div class="tips">
          <div class="tip">还差<span>1人</span>成团</div>
          <van-count-down
            ref="countDown"
            :time="countDownTime"
            format="mm 分 ss 秒"
          />
        </div>
        <div class="btn" @click="handleClick">去拼团</div>
      </div>
    </div>
    <div class="btns">
      <div class="btn" @click="singleBuy">
        <div class="price">￥{{ options.original_cost || 9.9 }}</div>
        <div class="text">单独购买</div>
      </div>
      <div class="btn create-group" @click="handleClick">
        <div class="price">￥{{ options.pay_money || 9.9 }}</div>
        <div class="text">去拼团</div>
      </div>
    </div>
  </div>
</template>
<script>
import { randomNum } from "@/utils";
export default {
  name: "group-buy-fixed",
  props: {
    view: {
      type: Object,
      default: () => ({}),
    },
    fixedViews: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      users: [],
      countDownTime: 10 * 60 * 1000,
    };
  },
  computed: {
    options() {
      return this.view.options || {};
    },
    fixedOffset() {
      let views = [...this.fixedViews].reverse();
      let len = views.length;
      let res = 0;
      for (let i = 0; i < len; i++) {
        let item = views[i];
        if (item.uid === this.view.uid) break;
        res += item?.options?.fixed_height || 0;
      }
      return `calc(${res}rem + var(--safe-area-inset-bottom) / 2)`;
    },
  },
  created() {
    this.loopUsers();
  },
  methods: {
    // 虚假拼团用户
    loopUsers() {
      this.users = [randomNum(60) + ".jpeg", randomNum(60) + ".jpeg"];
      let timer = setTimeout(() => {
        this.loopUsers();
        this.$emit("reset");
        this.$refs.countDown?.reset?.();
      }, this.countDownTime);
      this.$once("hook:beforeDestroy", () => {
        clearTimeout(timer);
      });
    },
    singleBuy() {
      const { original_cost, pay_money } = this.options;
      this.$dialog
        .confirm({
          title: "提示",
          message: `<span style="color: red;">¥${
            original_cost || pay_money
          }</span>预约已满，推荐直接参团优惠购买👇`,
          closeOnClickOverlay: true,
          confirmButtonColor: "#ff4b00",
          confirmButtonText: "去拼团",
        })
        .then(() => {
          this.handleClick();
        });
    },
    handleClick() {
      this.$emit("click");
    },
  },
};
</script>
<style lang="scss" scoped>
.group-buy-fixed {
  background-color: #fff;
  border-top: solid 1px #f3e7df;
  display: flex;
  flex-direction: column;
  min-width: 320px;
  max-width: 640px;
  width: 100%;
  height: 2.4888rem;
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 99;
  .info {
    background: #fff6ee;
    height: 1.1111rem;
    padding: 0 0.3333333rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .users {
      img {
        height: 0.8rem;
        width: 0.8rem;
        border-radius: 50%;
        border: 1px solid #e9e9e9;
        + img {
          margin-left: 4px;
        }
      }
    }
    &-right {
      display: flex;
      align-items: center;
      .tips {
        padding: 0 0.3333333rem;
        text-align: right;
        .tip {
          font-size: 0.32rem;
          span {
            color: #ff4b00;
            padding: 0 3px;
          }
        }
      }
      .van-count-down {
        color: #999;
        font-size: 0.3rem;
        line-height: 1;
        margin-top: 4px;
      }
      .btn {
        display: inline-block;
        width: 1.55rem;
        height: 0.7rem;
        line-height: 0.7rem;
        background: #ff4b00;
        color: #fff;
        text-align: center;
        font-size: 0.32rem;
        box-shadow: 0 2px 6px 0 rgb(255 75 0 / 30%);
        border-radius: 4px;
      }
    }
  }
  .btns {
    height: 1.3777rem;
    display: flex;
    align-items: center;
    padding: 0 0.3333333rem;
    .btn {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 0.45rem;
      background: #ff9b00;
      height: 1.1rem;
      border-radius: 4px 0 0 4px;
      &.create-group {
        background: #ff4b00;
        border-radius: 0 4px 4px 0;
      }
      .price {
        font-weight: 500;
      }
      .text {
        font-size: 0.31rem;
      }
    }
  }
}
</style>
